package net.liuyimin.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.util.List;
import java.util.concurrent.CountDownLatch;


/**
 * Zookeeper API测试
 */
public class ZkApiTest {
    public static void main(String[] args) throws  InterruptedException {
        /**
         * 获取zookeeper链接 : 借助CountDownLatch监控连接状态,保证成功连接后再执行API测试操作
         */
        // 创建一个计数器，用于监控链接状态
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ZooKeeper zooKeeper = null;
        try {
            zooKeeper = new ZooKeeper("127.0.0.1:2181", 30000, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    // 监控指定的事件
                    if (Event.KeeperState.SyncConnected.equals(event.getState())
                            && Event.EventType.None.equals(event.getType())) {
                        System.out.println("获取链接成功。。。。。。" + event);
                        // 释放计数器
                        countDownLatch.countDown();
                    }
                }
            });
            // 等待连接
            countDownLatch.await();

            /**
             * 节点新增
             * 创建一个节点的参数说明: 1-节点路径 2-节点内容 3-节点的访问权限 4-节点类型
             *
             * ===== ZooDefs：访问权限 =====
             * ZooDefs.Ids.CREATOR_ALL_ACL：创建者拥有所有访问权限
             * ZooDefs.Ids.READ_ACL_UNSAFE：任何人都可以读取该节点(任何人都可以读取该节点)
             * ZooDefs.Ids.OPEN_ACL_UNSAFE：允许所有客户端进行读写操作(任何人可以操作该节点)
             *
             * ===== CreateMode：节点类型 =====
             * CreateMode.PERSISTENT：持久节点
             * CreateMode.EPHEMERAL：临时节点
             * CreateMode.PERSISTENT_SEQUENTIAL：持久顺序节点
             * CreateMode.EPHEMERAL_SEQUENTIAL：临时顺序节点
             */
            zooKeeper.create("/test",    "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
//            zooKeeper.create("/test/aa", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//            zooKeeper.create("/test/cc", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
//            zooKeeper.create("/test/dd", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//            zooKeeper.create("/test/dd", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//            zooKeeper.create("/test/dd", "haha~~".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);

            /**
             * 判断节点是否存在
             */
            Stat stat = zooKeeper.exists("/test", true);
            if (stat != null){
                System.out.println("当前节点存在！" + stat.getVersion());
            } else {
                System.out.println("当前节点不存在！");
            }

            /**
             * 判断节点是否存在，同时添加监听
             */
            zooKeeper.exists("/test", event -> {
            });

            /**
             * 获取一个节点的数据
             */
            byte[] data = zooKeeper.getData("/test", false, null);
            System.out.println(new String(data));

            /**
             * 查询一个节点的所有子节点
             */
            List<String> children = zooKeeper.getChildren("/test", false);
            System.out.println(children);

            /**
             * 更新
             */
            zooKeeper.setData("/test", "wawa...".getBytes(), stat.getVersion());
            System.out.println(new String(zooKeeper.getData("/test", false, null)));

            /**
             * 删除一个节点
             */
            zooKeeper.delete("/test", -1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (zooKeeper != null){
                zooKeeper.close();
            }
        }
    }
    
}
